package com.bestcem.xm.user.grpc.service.converter;

import com.bestcem.xm.user.grpc.v1.common.GrowingIO;
import com.bestcem.xm.user.grpc.v1.common.IdRelation;
import com.bestcem.xm.user.service.dto.group.IdRelationDTO;
import com.bestcem.xm.user.service.dto.user.GrowingioDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 公共类转换
 *
 * @author wangyunyun <yunyun.wang@idiaoyan.com>
 * @version v1.0
 * @date 2022/6/30 13:08
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CommonConverter {

    public static IdRelation idRelationDto2Grpc(IdRelationDTO idRelation) {
        if ( idRelation == null ) {
            return null;
        }

        com.bestcem.xm.user.grpc.v1.common.IdRelation.Builder idRelation1 = IdRelation.newBuilder();

        if ( idRelation.getOldId() != null ) {
            idRelation1.setOldId( idRelation.getOldId() );
        }
        if ( idRelation.getNewId() != null ) {
            idRelation1.setNewId( idRelation.getNewId() );
        }

        return idRelation1.build();
    }

    public static List<IdRelation> idRelationDtosGrpcs(List<IdRelationDTO> idRelations) {
        if ( idRelations == null ) {
            return Collections.emptyList();
        }

        List<IdRelation> list = new ArrayList<>( idRelations.size() );
        for ( IdRelationDTO idRelationDTO : idRelations ) {
            list.add( idRelationDto2Grpc( idRelationDTO ) );
        }

        return list;
    }

    public static GrowingIO growingioDto2Grpc(GrowingioDTO growingio) {
        GrowingIO.Builder growinioBuilder = GrowingIO.newBuilder();
        if (!ObjectUtils.isEmpty(growingio.getLoginUserId())) {
            growinioBuilder.setLoginUserId(growingio.getLoginUserId());
        }
        if (!ObjectUtils.isEmpty(growingio.getCorporationPpl())) {
            growinioBuilder.setOrgCode(growingio.getCorporationPpl());
        }
        if (!ObjectUtils.isEmpty(growingio.getEnvironmentPpl())) {
            growinioBuilder.setEnvironment(growingio.getEnvironmentPpl());
        }
        if (!ObjectUtils.isEmpty(growingio.getIdentityPpl())) {
            growinioBuilder.setUserInnerDesc(growingio.getIdentityPpl());
        }
        if (!ObjectUtils.isEmpty(growingio.getInternalSetPpl())) {
            growinioBuilder.setOrgInnerDesc(growingio.getInternalSetPpl());
        }
        if (!ObjectUtils.isEmpty(growingio.getPricingPpl())) {
            growinioBuilder.setPackageName(growingio.getPricingPpl());
        }
        return growinioBuilder.buildPartial();
    }

}
